package book;

import java.util.Arrays;
import java.util.Scanner;

public class BookList {
    private Book[] books = new Book[10];
    private int bookListCapacity;
    private int bookListSize;

    public BookList() {
        books[0] = new Book("数字逻辑", "卫朝霞", "教材", 49);
        books[1] = new Book("数学之美", "吴军", "专业读物", 56);
        books[2] = new Book("朝花夕拾", "鲁迅", "文学著作", 65);
        this.bookListSize = 3;
        this.bookListCapacity = 10;
    }

    public void enlargeCapacity() {
        books = Arrays.copyOf(books, 2 * bookListCapacity);
        bookListCapacity *= 2;
    }

    public void addBook(Book book) {
        if (bookListCapacity == bookListSize) {//容量等于书的个数了 扩容
            enlargeCapacity();
        }
        books[bookListSize] = book;
        bookListSize++;
    }

    public int getBookListCapacity() {
        return bookListCapacity;
    }

    public void setBookListCapacity(int bookListCapacity) {
        this.bookListCapacity = bookListCapacity;
    }

    public Book getBookByPos(int pos) {
        return books[pos];
    }

    public String getBookNameByPos(int pos) {
        if (pos < 0 || pos > this.bookListSize) {
            return null;
        } else {
            return books[pos].getName();
        }
    }

    public Book getBookByName(String name) {
        for (int i = 0; i < bookListSize; i++) {
            String curBookName = books[i].getName();
            if (name.equals(curBookName)) {
                return books[i];
            }
        }
        return null;
    }

    public int getBookIndexByName(String name) {
        for (int i = 0; i < bookListSize; i++) {
            String curBookName = books[i].getName();
            if (name.equals(curBookName)) {
                return i;
            }
        }
        return -1;
    }

    public void setBookByPos(Book book, int pos) {
        books[pos] = book;
    }

    public void setBookListSize(int bookListSize) {
        this.bookListSize = bookListSize;
    }

    public int getBookListSize() {
        return bookListSize;
    }
}

